/**
 * 历史图片选择
 */

var img_html = "<div id='__DIV_IMAGE_MASK' style='display:block;position:absolute;z-index:8999;background-color:#000;filter:alpha(opacity=20);opacity:0.2;'></div><div id='__DIV_IMAGE' style='position:absolute;z-index:9000;'><dt><span>单击选择图片</span><a id='btn_cancel'>[ 取消 ]</a> <a id='btn_enter'>[ 确定 ]</a></dt><ul></ul></div>";
var $i_mask;
var $i_box;
var $i_lst;

$( "body" ).ready( function( e )
{
	img_init();
} );

function img_init()
{
	$( "body" ).append( img_html );
	$i_box = $( "#__DIV_IMAGE" );
	$i_mask = $( "#__DIV_IMAGE_MASK" );
	$i_lst = $i_box.find( ">ul:first" );

	$i_box.find( "#btn_cancel" ).click( function()
	{
		img_close();
	} );
	$i_box.find( "#btn_enter" ).click( function()
	{
		var a = new Array();
		$i_lst.find( "li[class=selected]" ).each( function()
		{
			a.push( $( this ).attr( "rel" ) );
		} );
		selected_enter( a );
		img_close();
	} );

	$i_box.hide();
	$i_mask.hide();
}

//窗口大小改变
$( window ).resize( function( e )
{
	if( $i_box.visible() )
	{
		img_open();
	}
} );

function img_open()
{
	var bw = $( "body" ).width();
	var bh = $( "body" ).height();
	var w = $i_box.width();
	var h = $i_box.height();

	$i_mask.width( bw );
	$i_mask.height( bh );
	$i_mask.move( 0, 0 );
	$i_box.move( ( bw - w ) / 2, ( bh - h ) / 2 * 0.8 );

	$i_lst.append( "<li id='NOTE' style='color:#ccc;'>正在加载图片...</li>" );
	$.post( "image_history.php", { width: 180, height: 150, page: 1 }, function( data )
	{
		$i_lst.find( "#NOTE" ).remove();
		$i_lst.append( data );

		$i_lst.find( "li" ).unbind( "click" );
		$i_lst.find( "li" ).bind( "click", img_click );
	} );

	//	$i_mask.show();
	//	$i_box.show();
	$i_box.slideDown( 150 );
	$i_mask.fadeIn( 150 );
}

function img_click( e )
{
	var selected = $( this ).attr( "class" ) == "selected";

	if( selected )
	{
		$( this ).removeClass( "selected" );
	}
	else
	{
		$( this ).addClass( "selected" );
	}
}

function img_close()
{
	//	$i_box.hide();
	//	$i_mask.hide();
	$i_lst.find( "li" ).remove();
	$i_box.slideUp( 150 );
	$i_mask.fadeOut( 150 );
}

var _img_selected_fn = null;

function set_selected_fn( fn )
{
	_img_selected_fn = fn;
}

function selected_enter( value )
{
	if( _img_selected_fn != null )
	{
		_img_selected_fn( value );
	}
}
